<?php

	/**
	 * @see Bike_Js_VarPart_Object
	 */
	require_once 'Bike/Js/VarPart/Object.php';
	
	
	class Bike_Js_Ext_Ajax_Request extends Bike_Js_VarPart_Object
	{
		protected $_part = 'request';

		protected function initDefaultSettings()
		{
			parent::initDefaultSettings();
		
			$js = $this->js();
			$this->setSettings(array(
				'url' => null,
				'success' => $this->getJsSuccess(),
				'failure' => $this->getJsFailure(),
			));
		}
		
		/**
		 * Get JS success
		 *
		 * @return Bike_Js_Interface_Js object
		 */
		protected function getJsSuccess()
		{
			if(!$this->getParamCache(__METHOD__))
			{
				$js = $this->js();
				$this->setParamCache(__METHOD__, $js->Ext->ResponseHandler->Success($js->response));
			}
			return $this->getParamCache(__METHOD__);
		}
		
		/**
		 * Get JS failure
		 *
		 * @return Bike_Js_Interface_Js object
		 */
		protected function getJsFailure()
		{
			if(!$this->getParamCache(__METHOD__))
			{
				$js = $this->js();
				$this->setParamCache(__METHOD__, $js->Ext->ResponseHandler->Failure($js->response));
			}
			return $this->getParamCache(__METHOD__);
		}

		public function _makeOptions(array $Args=null)
		{
			$options = array();
			$argsCount = sizeof($Args);
			if($argsCount)
			{
				if($argsCount == 1 && is_array($Args[0]) && isset($Args[0]['url']))
				{
					$options = $Args[0];
				}
				elseif($argsCount >= 1)
				{
					$options = array(
						'url' => $Args[0],
					);
				}
			}
			return $options;
		}
	}
